package com.zjdiepu.www.biz.system;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.system.BaseSalersmanMapper;
import com.zjdiepu.www.ibiz.system.IBaseSalersmanService;
import com.zjdiepu.www.model.system.dto.BaseSalersman;
import com.zjdiepu.www.model.system.ext.BaseSalersmanExt;

/**
 * Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月18日 15时43分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseSalersmanServiceImpl extends BaseService implements IBaseSalersmanService {
	public static final Logger logger = LoggerFactory.getLogger(BaseSalersmanServiceImpl.class);

	@Autowired
	private BaseSalersmanMapper baseSalersmanMapper;
	
	@Override
	public void saveUpdateAndDelete(BaseSalersman baseSalersman, OperateType operType) {
		switch (operType) {
		case add:
			baseSalersman.initInsertData(baseSalersman.getModifier(), baseSalersman.getModifierName());
			baseSalersmanMapper.insert(baseSalersman);
			break;
		case edit:
			baseSalersman.initUpdateData(baseSalersman.getModifier(), baseSalersman.getModifierName());
			// 影响的记录数
			int updateCount = baseSalersmanMapper.updateByPrimaryKeySelective(baseSalersman);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			baseSalersman.initDeleteData(baseSalersman.getModifier(), baseSalersman.getModifierName());
			// 影响的记录数
			int deleteCount = baseSalersmanMapper.updateByPrimaryKeySelective(baseSalersman);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<BaseSalersman> selectByExt(BaseSalersmanExt baseSalersmanExt) {
		return baseSalersmanMapper.selectByExt(baseSalersmanExt);
	}
	
	@Override
	public int countByExt(BaseSalersmanExt baseSalersmanExt){
		return baseSalersmanMapper.countByExt(baseSalersmanExt);
	}
	
	@Override
	public BaseSalersman selectById(String id) {
		return baseSalersmanMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseSalersmanMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseSalersman baseSalersman) {
		return baseSalersmanMapper.countRefined(baseSalersman);
	}

	@Override
	public BaseSalersman selectRefined(BaseSalersman baseSalersman) {
		return baseSalersmanMapper.selectRefined(baseSalersman);
	}
	
	@Override
	public int deletedRefined(BaseSalersman baseSalersman) {
		return baseSalersmanMapper.deletedRefined(baseSalersman);
	}

	@Override
	public boolean repeat(BaseSalersman baseSalersman) {
		int count = countRefined(baseSalersman);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseSalersman.getId())) {
			return true;
		}
		BaseSalersman dbItem = selectRefined(baseSalersman);
		if(null == dbItem) {
			return false;
		}
		return !baseSalersman.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return baseSalersmanMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<BaseSalersman> baseSalersmans) {
		return baseSalersmanMapper.insertBatch(baseSalersmans);
	}

	@Override
	public int countMuiltList(BaseSalersmanExt baseSalersmanExt) {
		
		return baseSalersmanMapper.countMuiltList(baseSalersmanExt);
	}

	@Override
	public List<BaseSalersman> selectMuiltList(BaseSalersmanExt baseSalersmanExt) {
		return baseSalersmanMapper.selectMuiltList(baseSalersmanExt);
	}
}
